package edu.ccut.saturn.studio.saturndata.dialog;

import java.util.List;

import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;


import edu.ccut.saturn.studio.core.util.eclipse.SaturnDialogUtil;
import edu.ccut.saturn.studio.core.util.eclipse.SaturnValidation;
import edu.ccut.saturn.studio.saturndata.model.DataItem;
import edu.ccut.saturn.studio.core.util.eclipse.Messages;

public class DataItemRenameDialog extends Dialog {

	private Text text;
	private Button okbutton;
	private String name = "";  //$NON-NLS-1$
	private List<DataItem> dataItems = null;

	public DataItemRenameDialog(Shell parentShell, List<DataItem> dataItems) {
		super(parentShell);
		
		this.dataItems = dataItems;
	}

	@Override
	protected void createButtonsForButtonBar(Composite parent) {
		okbutton = createButton(parent, IDialogConstants.OK_ID, Messages.getString("DataItemRenameDialog.finish"), false);  //$NON-NLS-1$
		okbutton.setEnabled(false);
		createButton(parent, IDialogConstants.CANCEL_ID, Messages.getString("DataItemRenameDialog.cancel"), true);  //$NON-NLS-1$
	}

	@Override
	protected Control createDialogArea(Composite parent) {
		Composite container = (Composite) super.createDialogArea(parent);
		final GridLayout gridLayout = new GridLayout();
		gridLayout.marginHeight = 10;
		gridLayout.marginRight = 10;
		gridLayout.marginLeft = 10;
		gridLayout.marginBottom = 5;
		gridLayout.marginWidth = 3;
		gridLayout.marginTop = 20;
		gridLayout.numColumns = 2;
		container.setLayout(gridLayout);

		final Label label = new Label(container, SWT.NONE);
		label.setAlignment(SWT.CENTER);
		final GridData gd_label = new GridData(SWT.LEFT, SWT.BOTTOM, false, false);
		gd_label.heightHint = 15;
		gd_label.widthHint = 66;
		label.setLayoutData(gd_label);
		label.setText(Messages.getString("DataItemRenameDialog.newname"));  //$NON-NLS-1$

		text = new Text(container, SWT.BORDER);
		text.setTextLimit(50);
		final GridData gd_text = new GridData(SWT.FILL, SWT.CENTER, true, false);
		gd_text.heightHint = 16;
		text.setLayoutData(gd_text);
		text.setText(getName());

		text.addModifyListener(new ModifyListener() {
			public void modifyText(ModifyEvent modifyevent) {
				String fieldname = text.getText().trim();

				if (SaturnValidation.isEmpty(fieldname) || isSameName(fieldname) || !SaturnValidation.isValidProgramStr(fieldname)) {
					okbutton.setEnabled(false);
				} else {
					okbutton.setEnabled(true);
				}
				if(fieldname.length()== 50) {
					MessageDialog.openInformation(null,	(Messages.getString("DataItemRenameDialog.remend")), (Messages.getString("DataItemRenameDialog.newnamelengthlimit")));      //$NON-NLS-1$ //$NON-NLS-2$
				}
				if(SaturnValidation.isEmpty(fieldname)) {
					MessageDialog.openInformation(null,	(Messages.getString("DataItemRenameDialog.remend")), (Messages.getString("DataItemRenameDialog.newnamecannotempty")));    //$NON-NLS-1$ //$NON-NLS-2$
				}	
				if(!SaturnValidation.isValidProgramStr(fieldname)) {
					MessageDialog.openInformation(null,	(Messages.getString("DataItemRenameDialog.remend")), (Messages.getString("DataItemRenameDialog.newnameconstitute")));   //$NON-NLS-1$ //$NON-NLS-2$
			    }
			}	
		});

		return container;
	}
	

	private boolean isSameName(String fieldname) {
		for (DataItem fieldVo : dataItems) {
			String dataItemName = fieldVo.getName();
			if (dataItemName.equals(fieldname)) {
				SaturnDialogUtil.openInformation(super.getShell(), 
						(Messages.getString("DataItemRenameDialog.fieldnameexist")));   //$NON-NLS-1$

				return true;
			}
		}

		return false;
	}

	protected void buttonPressed(int buttonId) {
		if (buttonId == IDialogConstants.CANCEL_ID) {
			close();
		}
		if (buttonId == IDialogConstants.OK_ID) {
			name = text.getText().trim();
		}

		super.buttonPressed(buttonId);

	}

	@Override
	protected Point getInitialSize() {
		return new Point(500, 134);
	}

	protected void configureShell(Shell newShell) {
		super.configureShell(newShell);
		newShell.setText(Messages.getString("DataItemRenameDialog.alterfieldname"));  //$NON-NLS-1$
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}
